package com.cskaoyan.wx.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.bean.System;
import com.cskaoyan.mapper.*;
import com.cskaoyan.service.ConfigServiceImpl;
import com.cskaoyan.wx.vo.HomeIndexVO;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.ArrayList;
import java.util.List;

@Service
public class HomeServiceImpl implements HomeService {


    @Autowired
    AdMapper adMapper;
    @Autowired
    BrandMapper brandMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    ConfigMapper configMapper;
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    GrouponRulesMapper grouponRulesMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Autowired
    TopicMapper topicMapper;

    @Override
    public HomeIndexVO getIndex() {

        List<ConfigServiceImpl.Pair> keyNameList = new ArrayList<>();
//        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_catlog_goods"));
//        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_catlog_list"));
        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_index_brand"));
        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_index_hot"));
        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_index_new"));
        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_index_topic"));
//        keyNameList.add(new ConfigServiceImpl.Pair("cskaoyan_mall_wx_share"));

        ConfigWX configWX = configMapper.getWXConfig(keyNameList);


        PageHelper.startPage(1, 10);
        HomeIndexVO homeIndexVO = new HomeIndexVO();
        AdExample adExample = new AdExample();
//        adExample.setOrderByClause("update_time desc");
        AdExample.Criteria adCriteria = adExample.createCriteria();
        adCriteria.andPositionEqualTo((byte) 1);
        adCriteria.andDeletedEqualTo(false);
        List<Ad> ads = adMapper.selectByExample(adExample);
        homeIndexVO.setBanner(ads);
        PageHelper.clearPage();

        int brand = Integer.parseInt(configWX.getCskaoyanmall_wx_index_brand());
        PageHelper.startPage(1, brand);
        BrandExample brandExample = new BrandExample();
//        brandExample.setOrderByClause("update_time desc");
        BrandExample.Criteria brandExampleCriteria = brandExample.createCriteria();
        brandExampleCriteria.andDeletedEqualTo(false);
        List<Brand> brands = brandMapper.selectByExample(brandExample);
        homeIndexVO.setBrandList(brands);
        PageHelper.clearPage();


        PageHelper.startPage(1, 15);
        CategoryExample categoryExample = new CategoryExample();
//        categoryExample.setOrderByClause("update_time desc");
        CategoryExample.Criteria categoryExampleCriteria = categoryExample.createCriteria();
        categoryExampleCriteria.andDeletedEqualTo(false);
        categoryExampleCriteria.andLevelEqualTo("L1");
        List<Category> categories = categoryMapper.selectByExample(categoryExample);
        homeIndexVO.setChannel(categories);
        PageHelper.clearPage();


        PageHelper.startPage(1, 3);
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria couponExampleCriteria = couponExample.createCriteria();
        couponExampleCriteria.andDeletedEqualTo(false);
        couponExampleCriteria.andTypeEqualTo((short) 0);
        couponExampleCriteria.andStatusEqualTo((short) 0);
        List<Coupon> coupons = couponMapper.selectByExample(couponExample);
        homeIndexVO.setCouponList(coupons);

        List<HomeIndexVO.Groupon> groupons = grouponRulesMapper.getRuleAndGoods();
        homeIndexVO.setGrouponList(groupons);


        int hot = Integer.parseInt(configWX.getCskaoyanmall_wx_index_hot());
        PageHelper.startPage(1, hot);
        GoodsExample goodsExample = new GoodsExample();
        GoodsExample.Criteria goodCriteria = goodsExample.createCriteria();
        goodCriteria.andIsHotEqualTo(true);
        goodCriteria.andIsOnSaleEqualTo(true);
        goodCriteria.andDeletedEqualTo(false);
        List<Goods> hotGoods = goodsMapper.selectByExample(goodsExample);
        homeIndexVO.setHotGoodsList(hotGoods);

        int newGoods = Integer.parseInt(configWX.getCskaoyanmall_wx_index_new());
        PageHelper.startPage(1, newGoods);
        goodsExample.clear();
        GoodsExample.Criteria hotCriteria = goodsExample.createCriteria();
        hotCriteria.andIsNewEqualTo(true);
        hotCriteria.andIsOnSaleEqualTo(true);
        hotCriteria.andDeletedEqualTo(false);
        List<Goods> newGoodsList = goodsMapper.selectByExample(goodsExample);
        homeIndexVO.setNewGoodsList(newGoodsList);


        int topic = Integer.parseInt(configWX.getCskaoyanmall_wx_index_topic());
        PageHelper.startPage(1, topic);
        TopicExample topicExample = new TopicExample();
        TopicExample.Criteria topicCriteria = topicExample.createCriteria();
        topicCriteria.andDeletedEqualTo(false);
        List<Topic> topics = topicMapper.selectByExample(topicExample);
        homeIndexVO.setTopicList(topics);


        List<HomeIndexVO.FloorGoods> goods = categoryMapper.getFloorGoodsList();
        homeIndexVO.setFloorGoodsList(goods);

        return homeIndexVO;
    }
}
